/**
 * 
 */
package com.jimet.authorization;

import java.sql.ResultSet;
import java.util.Vector;

import com.jimet.db.JimetAuthorizationDB;

/**
 * @author
 *
 */
/**
 * @author Administrator
 *
 */
/**
 * @author Administrator
 *
 */
public class JimetAuthorizations {

	private int pos;
	public Vector<JimetAuthorization> authVec;

	
	/**
	 * @throws Exception
	 * constructer
	 */
	public JimetAuthorizations() throws Exception 
	{
	}

	
	/**
	 * @return the size of authvec of jimetauthorizations
	 */
	public int size()
	{
		if(this.authVec == null)
			return 0;
		else
			return this.authVec.size();
	}
	
	
	/**
	 * @param authID
	 * @return
	 * checks if this.authVec has the auth with the given authid
	 */
	public boolean hasThisAuth(int authID)
	{
		if(!(this.authVec == null)) {

		for(int i=0;i<this.authVec.size();i++)
		{
			JimetAuthorization jauth = (JimetAuthorization) this.authVec.elementAt(i);
			if(jauth.getId() == authID)
				return true;
		}
		}		
		return false;		
	}

	/**
	 * @param authShortCode
	 * @return
	 * checks if this.authVec has the auth with the given authShortCode
	 */
	public boolean hasThisAuth(String authShortCode)
	{
		if(!(this.authVec == null)) {
		for(int i=0;i<this.authVec.size();i++)
		{
			JimetAuthorization jauth = (JimetAuthorization) this.authVec.elementAt(i);
			if(jauth.getShortCode().equals(authShortCode)) {
				return true;
			}
		}
		}
		return false;		
	}	
	
	
	/**
	 * @param jauth
	 * adds a constructed auth to this.authVec(not phisical)
	 */
	public void addAuth(JimetAuthorization jauth)
	{

		if(this.authVec == null)
		{
			this.pos = -1;
			this.authVec = new Vector<JimetAuthorization>();
		}
		this.authVec.add(jauth);
	}
	public void removeAuth(JimetAuthorization jauth)
	{

		for(int i=0;i<this.authVec.size();i++)
		{
			JimetAuthorization anObject = (JimetAuthorization) this.authVec.elementAt(i);
			if(anObject.getId() == jauth.getId())
				this.authVec.removeElementAt(i);
		}
		
	}
	
	/**
	 * @throws Exception
	 * loads all jimetauths from db usr_right
	 */
	public void load() throws Exception
	{
		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		ResultSet rs = jimdb.getAllAuthorizationIds();
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.load(String.valueOf(rs.getInt(1)));
			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
	}

	
	/**
	 * @param strSort
	 * @param strLike
	 * @param strSortBy
	 * @param strSearchBy
	 * @throws Exception
	 * parametrik version of load method
	 */
	public void search(String strSort, String strLike, String strSortBy,String strSearchBy) throws Exception {
		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		ResultSet rs = jimdb.getAuthSearchResults(strSort, strLike, strSortBy, strSearchBy);
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.load(String.valueOf(rs.getInt(1)));
			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
		
	}

	
	/**
	 * @param strArrAuthIDs
	 * @throws Exception
	 * loads the auths with the given authids in array
	 */
	public void loadByAuthIDs(String [] strArrAuthIDs) throws Exception
	{
		
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();		
		ResultSet rs = jimdb.getAuthsByID(strArrAuthIDs);
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
	}

	
	/**
	 * @param userID
	 * @throws Exception
	 * loads the auths that belongs to the given user`s id
	 */
	public void loadByUserID(String userID) throws Exception
	{
		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		ResultSet rs = jimdb.getUserAuthorizationSet(Integer.parseInt(userID));
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
	}
	
	public void loadByTypeId(String typeId) throws Exception
	{
		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		ResultSet rs = jimdb.getGroupAuthorizationSetByType(Integer.parseInt(typeId));
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
	}
	
	public void loadByGroupId(String groupId) throws Exception
	{
		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		ResultSet rs = jimdb.getGroupAuthorizationSet(Integer.parseInt(groupId));
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		
		jimdb.disconnect();
	}
	
	public void loadNotIn(Vector<String> notInIds)  throws Exception 
	{		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();		
		ResultSet rs = jimdb.getAllAuthsSetNotIn(notInIds);
		
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		jimdb.disconnect();
	}
	
	public void loadNotInAndIn(Vector<String> notInIds, String inStr)  throws Exception 
	{		
		this.pos = -1;
		this.authVec = new Vector<JimetAuthorization>();
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();		
		ResultSet rs = jimdb.getAllAuthsSetNotInAndIn(notInIds,inStr);
		
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			jauth.setAuthType(rs.getString(4));

			this.authVec.add(jauth);
		}
		jimdb.disconnect();
	}

	
	/**
	 * @param userID
	 * @throws Exception
	 * deletes(removes) all user rights from usr_right_owner(not usr_right)
	 */
	public void deleteUserRights(String userID) throws Exception
	{
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		jimdb.deleteUserRights(Integer.parseInt(userID));
		jimdb.disconnect();
	}

	/**
	 * @param userID
	 * @param rightID
	 * @throws Exception
	 * gives a right to the user(inserts into usr_right_owner)
	 */
	public void addUserRight(String userID, String rightID) throws Exception
	{
		
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		jimdb.addUserRight(Integer.parseInt(userID),Integer.parseInt(rightID));
		jimdb.disconnect();
		
	}
	
	public boolean contains(JimetAuthorization lookFor)
	{
		if(this.authVec != null)
		{
			for(int i=0;i<this.authVec.size();i++)
			{
				JimetAuthorization anObject = (JimetAuthorization) this.authVec.elementAt(i);
				if(anObject.getId() == lookFor.getId())
					return true;
			}
		}
		return false;
	}
	
	/**
	 * @throws Exception
	 * deletes ALL auths physically from db(usr_right)!!!
	 */
	public void delete() throws Exception
	{
		for(int i=0;i<authVec.size();i++)
		{
			JimetAuthorization jauth = (JimetAuthorization) authVec.elementAt(i);
			jauth.delete();
		}
	}
	
	/**
	 * re-sets the pos for a new loop
	 */
	public void reset()
	{
		this.pos = -1;
	}

	public boolean next()
	{
		if(this.authVec == null)
			return false;
		
		pos++;
		return  (pos < this.authVec.size());
	}

	public JimetAuthorization getNext()
	{
		return (JimetAuthorization) this.authVec.elementAt(this.pos);
	}
	


}
